package com.tuo51.foudation.utils;


import com.tuo51.foudation.constant.APPConstants;
import org.joda.time.DateTime;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.DecimalFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class PaymentUtils {

    public static synchronized String createOrderNo(String userId, APPConstants.PayType payType){

//        String _userId = String.valueOf(userId);
//        int max = Math.max(_userId.length(), 11);
//        int diff = max - _userId.length();

//        String timeMillis = String.valueOf(System.nanoTime());

        String p1 = DateTime.now().toString("yyyyMMdd");
        String p2 = String.valueOf(payType.getValue());
//        String p3 = userId + timeMillis.substring(timeMillis.length() - diff);

        String p3 = userId;

        String p4 = String.valueOf((long) (Math.random() * Long.MAX_VALUE)).substring(0, 10);

        String tradeNo = p1 + p2 + p3 + p4;

        return tradeNo;
    }

    /**
     * 分转换成元 保留2位小数
     * @param price
     * @return
     */
    public static String fen2yuan(int price){
        DecimalFormat df = new DecimalFormat("0.00");
        double yuan = Double.valueOf(price) / 100D;
        return df.format(yuan);
    }

    public static int yuan2fen(String price){
        Double d = Double.valueOf(price) * 100;
        return d.intValue();

    }

    public static Map<String,String> xml2Map(String xml){

        Document doc = Jsoup.parse(xml);
        Elements elements = doc.getElementsByTag("xml").get(0).getAllElements();

        Map<String,String> resultMap = new HashMap<>();
        for (Element element : elements) {
            resultMap.put(element.nodeName(), element.text());
        }

        resultMap.remove("xml");
        return resultMap;
    }

    public static String getLocalIP() {
        String sIP = "";
        InetAddress ip = null;
        try {
            boolean bFindIP = false;
            Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
                    .getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                if (bFindIP) {
                    break;
                }
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();

                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = (InetAddress) ips.nextElement();
                    if (!ip.isLoopbackAddress()
                            && ip.getHostAddress().matches(
                            "(\\d{1,3}\\.){3}\\d{1,3}")) {
                        bFindIP = true;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != ip) {
            sIP = ip.getHostAddress();
        }
        return sIP;
    }

    public static Map<String, String> getRequestParamsMap(HttpServletRequest request) {

        Map<String, String> paramsMap = new HashMap<>();
        Map<String, String[]> reqMap = request.getParameterMap();
        for (String key : reqMap.keySet()) {
            String[] ary = reqMap.get(key);
            if (ary != null && ary.length > 0) {
                paramsMap.put(key, ary[0]);
            }
        }
        return paramsMap;
    }


}
